<?php
class Default_Model_SubMenu extends Zend_Db_Table {
	protected $_name    = 'sub_menu';
	protected $_primary = 'id';
	protected $_dependentTables = array('Default_Model_MenuTour');
	
	// creat item in select box
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options == null){
			$select = $db->select()
			             ->from('sub_menu AS sm',array('id','name','parents'))
			             ->order('sm.order ASC');
			$result  = $db->fetchAll($select);
				
		}
	
		if($options['task'] == 'admin-edit'){
			$id = $arrParam['id'];
			$select = $db->select()
			             ->from('sub_menu AS sm',array('id','name','parents'))
			             ->where('id != ?', $id, INTEGER )
			             ->order('sm.order ASC');
				
			$result  = $db->fetchAll($select);
				
		}
	
		$system = new MyZend_System_Recursive($result);
		$result = $system->buildArray(0);
		$tmp = array('id'=>0,'name'=>'Root Menu','level'=>1,'order'=>1,'parents'=>0);
		array_unshift($result,$tmp);
	
		return $result;
		
	}
	
	public function countItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		->from('sub_menu AS sm', array('COUNT(sm.id)  AS totalItem'));
	
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('sm.name LIKE ?',$keywords,STRING);
		}
	
		$result = $db->fetchOne($select);
		return $result;
	
	}
	
	public function listItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-list') {
			$db = Zend_Registry::get('connectDb');
			$paginator = $arrParam['paginator'];
			$ssFilter = $arrParam['ssFilter'];
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
			             ->from('sub_menu AS sm', array('id', 'name', 'status', 'order'));
			
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
	
			if($paginator['itemCountPerPage']>0) {
				$page     = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
	
			// seach keyword
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('sm.name LIKE ?',$keywords,STRING);
			}
			$result = $db->fetchAll($select);
		}
	
		return $result;
	}
	
	// change status
	public function changeStatus($arrParam = null, $options = null) {
			
		// Multi change
		$cid = $arrParam['cid'];
		if(count($cid) > 0) {
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
				
			$ids   = implode(',', $cid);
			$where = 'id IN (' . $ids . ')';
			$data  =  array('status'=>$status);
			$this->update($data, $where);
		}
			
		// change only one item
		if($arrParam['id'] > 0) {
			$status = $arrParam['type'];
			$data   = array('status'=>$status);
			$where  = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	 
	// orderItems
	public function sortItems($arrParam = null, $options = null) {
		$order = $arrParam['order'];
		$cid   = $arrParam['cid'];
		if(count($cid) > 0) {
			foreach ($cid as $key => $value) {
				$where = 'id = ' . $value;
				$data  = array('order' => $order[$value]);
				$this->update($data, $where);
			}
	
		}
	}
	 
	// delete item
	public function deleteItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-delete') {
			$where = ' id = ' . $arrParam['id'];
			$this->delete($where);
		}
		 
		if($options['task'] == 'admin-multi-delete') {
			$cid = $arrParam['cid'];
			if(count($cid) > 0) {
				$ids   = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}
	 
	// save item
	public function saveItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-add') {
			$row = $this->fetchNew();
			$row->name          = $arrParam['menu_name'];
			$row->created 		= date("Y-m-d");
			$row->created_by 	= 1;
			$row->status 		= $arrParam['status'];
			$row->order 		= $arrParam['order'];
			$row->parents       = $arrParam['parents'];
			
			$row->save();

		}
	
		if($options['task'] == 'admin-edit') {
			$id    = $arrParam['id'];
			$where = 'id = ' . $id;
	
			$row =  $this->fetchRow($where);
	
			$row->name = $arrParam['menu_name'];
			$row->modified      = date("Y-m-d");
			$row->modified_by   = 1;
			$row->status 		= $arrParam['status'];
			$row->order 		= $arrParam['order'];
	        $row->parents       = $arrParam['parents'];
	        
			$row->save();
		}
	}
	
	// get info item
	public function getItem($arrParam = null, $options = null) {
		if(($options['task'] == 'admin-info') || ($options['task'] == 'admin-edit')) {
			$where  = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
			if($result['parents'] > 0){
				$result['parents'] = $this->getParent($result['parents']);
			}
	        
		}
		 
		return $result;
	}
	 
	 
	// find menu contain category
	public function getParent($parents_id){
		if(isset($parents_id)){
			$where  = 'id = ' . $parents_id;
			$result = $this->fetchRow($where)->toArray();
			return $result;
		}
	}
	
}